package com.coocaa.aries.core.tool.date;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * @author bijiahao
 * @date : 2019/6/13.
 * @description 时间工具类
 * @link {https://mp.weixin.qq.com/s?__biz=MzAxODcyNjEzNQ==&mid=2247487399&idx=1&sn=f392e49f53905e24cdc12fc8e76129a7&chksm=9bd0a23faca72b293f9e182ea9faf815936c93f927196feb929f357deb43aa02fd4538626bf2&scene=21#wechat_redirect}
 */
public class DateUtil {

    private static ThreadLocal<SimpleDateFormat> threadLocal = new ThreadLocal<SimpleDateFormat>() {
        @Override
        protected SimpleDateFormat initialValue() {
            return new SimpleDateFormat();
        }
    };

    private DateUtil() {

    }

    public static String getCurDateStr(String dateFormat) {
        return format(getCurDate(), dateFormat);
    }

    public static String toLongString(Date date) {
        return format(date, "yyyy-MM-dd HH:mm:ss");
    }

    public static String toShortString(Date date) {
        return format(date, "yyyy-MM-dd");
    }

    public static Date parseStr2Date(String dateStr) {
        String pattern = "yyyy-MM-dd HH:mm:ss";
        try {
            return (new SimpleDateFormat(pattern)).parse(dateStr);
        } catch (ParseException var4) {
            var4.printStackTrace();
            return null;
        }
    }

    public static String date2Str(String pattern, Date date) {
        String time = format(date, pattern);
        return time;
    }

    /**
     * 获取前一天
     *
     * @return
     */
    public static Date getPreviouDate(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DAY_OF_MONTH, -1);
        date = calendar.getTime();
        return date;
    }

    /**
     * 获取后几天
     *
     * @return
     */
    public static Date getNextDate(Date date, int days) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DAY_OF_MONTH, days);
        date = calendar.getTime();
        return date;
    }

    /**
     * 获取时间的前几分钟
     *
     * @param minutes
     * @return
     */
    public static Date getPreviouMinute(Date date, int minutes) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.MINUTE, -minutes);
        return calendar.getTime();
    }

    /**
     * 获取时间的前几个月
     *
     * @param months
     * @return
     */
    public static Date getPreviouMonth(Date date, int months) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.MONTH, -months);
        return calendar.getTime();
    }

    /**
     * 获取相差的小时数
     *
     * @param date
     * @return
     */
    public static Integer getHoursDiff(Date date) {
        long current = System.currentTimeMillis();
        long start = date.getTime();
        int hours = (int) ((current - start) / (1000 * 60 * 60));
        return hours;
    }

    /**
     * 获取之后几小时的时间
     *
     * @param date
     * @param hours
     * @return
     */
    public static Date getNextHoursDate(Date date, int hours) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.HOUR_OF_DAY, hours);
        date = calendar.getTime();
        return date;
    }

    /**
     * 获取小时数
     *
     * @param date
     * @return
     */
    public static String getCurrentHour(Date date) {
        return format(date, "HH");
    }

    public static String getDefaultCurrentTime() {
        return getCurrentDateStr("yyyy-MM-dd HH:mm:ss");
    }

    public static Date getCurDate() {
        return new Date();
    }

    public static String getCurrentDateStr(String pattern) {
        return format(new Date(), pattern);
    }

    private static String format(Date date, String pattern) {
        SimpleDateFormat sdf = threadLocal.get();
        sdf.applyPattern(pattern);
        return sdf.format(date);
    }
}
